﻿using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SHomeWorkshop.LunarPresentation.Widegets
{
    public class ExpressionLayerButton : Button
    {
        public ExpressionLayerButton(Expressions.Expression correspondingExpression)
        {
            basePanel = new StackPanel();
            basePanel.Orientation = Orientation.Horizontal;

            textBlock = new TextBlock() { VerticalAlignment = System.Windows.VerticalAlignment.Center };

            if (correspondingExpression != null)
            {
                this.correspondingExpression = correspondingExpression;
                textBlock.Text = correspondingExpression.ChineseName;
            }
            else
            {
                textBlock.Text = "{0}";
            }

            basePanel.Children.Add(textBlock);

            img = new Image() { SnapsToDevicePixels = true };
            img.Width = 12; img.Height = 16;
            img.VerticalAlignment = System.Windows.VerticalAlignment.Center;

            bitmapImg = new BitmapImage(
                new Uri("pack://application:,,,/LunarPresentation;component/Resources/Icons/RightTriangle.png"));
            img.Source = bitmapImg;

            basePanel.Children.Add(img);

            this.Content = basePanel;


            if (correspondingExpression is Expressions.SubPanel)
            {
                this.Background = Brushes.LightSlateGray;
                this.Foreground = Brushes.White;
            }
            else
            {
                this.Background = Brushes.LightGray;
                this.Foreground = Brushes.Black;
            }

            this.Click += new System.Windows.RoutedEventHandler(ExpressionLayerButton_Click);

            if (correspondingExpression.CommentText != null && correspondingExpression.CommentText.Length > 0)
            {
                if (correspondingExpression.CommentText.Length > 10)
                {
                    this.ToolTip = correspondingExpression.CommentText.Substring(0, 10) + "…";
                }
                else
                {
                    this.ToolTip = correspondingExpression.CommentText;
                }
            }

            if (correspondingExpression.CommentText != null &&
                correspondingExpression.CommentText.Length > 0)
            {
                this.BorderBrush = Brushes.Red;
            }
        }

        void ExpressionLayerButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (correspondingExpression != null)
            {
                if (correspondingExpression.Editor.SelectedExpression != correspondingExpression)
                {
                    // 似乎可以不考虑这个公式在不在Expander的折叠面板中。——好像那种情况是不可能出现的。
                    correspondingExpression.Editor.SelectedExpression = correspondingExpression;
                }
                else
                {
                    Globals.TryToShowExpressionAtTopLeft(correspondingExpression);
                }
            }
        }

        private bool isLastLayer = false;

        /// <summary>
        /// 如果是列表中最后一个按钮。
        /// </summary>
        public bool IsLastLayer
        {
            get { return isLastLayer; }
            set
            {
                isLastLayer = value;
                if (isLastLayer)
                {
                    img.Visibility = System.Windows.Visibility.Collapsed;
                    this.textBlock.FontWeight = System.Windows.FontWeights.Bold;
                    this.textBlock.Foreground = Brushes.DarkBlue;
                }
                else
                {
                    img.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }

        private Expressions.Expression correspondingExpression;

        public Expressions.Expression CorrespondingExpression
        {
            get { return correspondingExpression; }
            set { correspondingExpression = value; }
        }

        private Image img;

        private BitmapImage bitmapImg;

        private StackPanel basePanel;

        public StackPanel BasePanel
        {
            get { return basePanel; }
            set { basePanel = value; }
        }

        private TextBlock textBlock;
    }
}
